<template>
  <div>
    <upload-excel :on-success="success" />
  </div>
</template>

<script>

const userRelations = {
  '入职日期': 'timeOfEntry',
  '手机号': 'mobile',
  '姓名': 'username',
  '转正日期': 'correctionTime',
  '工号': 'workNumber'
}
function formatDate(numb, format) {
  const time = new Date((numb - 1) * 24 * 3600000 + 1)
  time.setYear(time.getFullYear() - 70)
  const year = time.getFullYear() + ''
  const month = time.getMonth() + 1 + ''
  const date = time.getDate() - 1 + ''
  if (format && format.length === 1) {
    return year + format + month + format + date
  }
  return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
}
function changeCh(arr) {
  return arr.map(itm => {
    const newObj = {}
    Object.keys(userRelations).forEach(key => {
      if (key === '入职日期' || key === '转正日期') {
        newObj[userRelations[key]] = new Date(formatDate(itm[key], '/'))
      } else {
        newObj[userRelations[key]] = itm[key]
      }
    })
    return newObj
  })
}
import { importExcel } from '@/api/employees'
export default {
  methods: {
    async success({ header, results }) {
      const data = changeCh(results)
      console.log(data)
      await importExcel(data)
      this.$message('导入成功')
      this.$router.back()
    }
  }
}
</script>

<style>

</style>
